package com.dl698.parse;

import com.dl698.axdr.*;
import com.dl698.bean.FrameData;
import com.dl698.tools.ResultCode;

import java.util.HashMap;
import java.util.Map;

/**
 * ParseAuthResponse类
 * 实现认证响应解析功能
 * 
 * @author wind
 * @version 1.1.1
 */
public class ParseAuthResponse {

    public static String parseSecurityResponse(Security_Response security_response) {
        FrameData frameData = new FrameData();

        switch (security_response.getAppUnit().getChoiceIndex().getValue()) {
            case 0:
                Map<String, Object> plain_unit = new HashMap<>();
                plain_unit.put("plain_data", security_response.getAppUnit().getAppUnitPalin().toString());
                if (security_response.getDataValid().isUsed()) {
                    plain_unit.put("mac", security_response.getDataValid().getValue().getMAC().toString());
                }
                frameData.setData(plain_unit);
                frameData.setCode(ResultCode.SUCCESS);
                break;
            case 1:
                Map<String, Object> cipher_unit = new HashMap<>();
                cipher_unit.put("cipher_data", security_response.getAppUnit().getAppUnit().toString());
                if (security_response.getDataValid().isUsed()) {
                    cipher_unit.put("mac", security_response.getDataValid().getValue().getMAC().toString());
                }
                frameData.setData(cipher_unit);
                frameData.setCode(ResultCode.SUCCESS);
                break;
            case 2:
                Map<String, Object> dar = new HashMap<>();
                dar.put("dar", security_response.getAppUnit().getDar().toString());
                frameData.setData(dar);
                frameData.setCode(ResultCode.SUCCESS);
                break;
        }

        return frameData.toJsonString();
    }

    public static String parseConnectResponse(ConnectResponseInfo connectResponseInfo) {
        FrameData frameData = new FrameData();
        Map<String, Object> connect_data = new HashMap<>();

        connect_data.put("rn", connectResponseInfo.getSecurityData().getValue().getRn().toString());
        connect_data.put("sign", connectResponseInfo.getSecurityData().getValue().getSign().toString());

        frameData.setData(connect_data);
        frameData.setCode(ResultCode.SUCCESS);
        return frameData.toJsonString();
    }
}